package ookweb;

import java.sql.*;
import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jorm.Session;
import org.jorm.exception.JormptException;

import com.build.SessionBuilder;
import com.manager.service.news.CategoryService;
import com.web.model.Category;

public class hpd extends HttpServlet {
	/**
	 * Constructor of the object.
	 */
	public hpd() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String pages = request.getParameter("pages");
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		if ("".equals(pages) || pages == null) {
			pages = "1";
		}
		String xml = "";
		xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
		xml = xml + "<bcaster autoPlayTime=\"10\">\n";
		Session session;
		String path = request.getContextPath();
		String serverName = request.getServerName();
		if (path != null && path.equals("/")) {
			path = "";
		}
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path ;

		try {
			session = SessionBuilder.oracleSession();

			Connection conn = session.connection();
			String sql = " select * from  TW_HDP where pages=" + pages;
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			try {
				while (rs.next()) {
					xml = xml + "<item item_url=\"" + rs.getString("picurl")
							+ "\" link=\"" +basePath+ rs.getString("linkurl")
							+ "\" itemtitle=\"\"></item>\n";
				}
			} catch (SQLException e) {
				xml = xml
						+ "<item item_url=\"\" link=\"\" itemtitle=\"\"></item>\n";
			}
			
			CategoryService cateService = new CategoryService();
			List<Category> categorys = cateService.findShow();
			request.getSession().getServletContext().setAttribute("categorys", categorys);
			
			rs.close();
			ps.close();
			session.close();
		} catch (JormptException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		xml = xml + "</bcaster>\n";
		PrintWriter out = response.getWriter();
		out.println(xml);
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
